import Vue from "vue";
import Config from "@/config/index";
// const dataName = Config.response.dataName;
const statusName = Config.response.statusName;
const statusCode = Config.response.statusCode;

export function setTitle(title) {
  window.document.title = title || "admin";
}

/**
 *
 *
 * @export
 * @param {*} {
 *   row,
 *   column,
 *   rowIndex,
 *   columnIndex,
 *   colMergeIndex = 0,
 *   spanArr
 * }
 * @returns
 */
export function cellMerge({
  // row,
  // column,
  rowIndex,
  columnIndex,
  colMergeIndex = 0,
  spanArr
}) {
  if (columnIndex === colMergeIndex) {
    const _row = spanArr[rowIndex];
    const _col = _row > 0 ? 1 : 0;
    return {
      rowspan: _row,
      colspan: _col
    };
  }
}

/**
 * 获取接口结束后，提示信息
 *
 * @export
 * @param {*} { result, message = "操作成功" }
 */
export function doneMessage({ result, message = "操作成功" }) {
  let type = null;
  if (result[statusName] === statusCode.ok) {
    type = "success";
  } else {
    type = "error";
    message = "操作失败";
  }
  Vue.prototype.$message({
    type,
    message
  });
}
/**
 * 清除form数据
 *
 * @export
 * @param {String} form_name
 */
export function resetForm(form_name) {
  const formRef = this.$refs[form_name];
  formRef.resetFields();
  this.$nextTick(() => {
    formRef.clearValidate();
  });
}

/**
 * 转换多级表头为一维数组
 *
 * @export
 * @param {*} arr
 */
export function oneDimensionalArr({
  arr,
  children = "children",
  label = "label",
  prop = "prop"
}) {
  if (!Array.isArray(arr)) return;
  let result = [];

  function walker(arr, parent_label) {
    arr.forEach(element => {
      // debugger;
      let label_text = parent_label
        ? `${parent_label}-${element[label]}`
        : `${element[label]}`;
      if (!element[children]) {
        element[label] = label_text;
        result.push({
          [label]: element[label],
          [prop]: element[prop]
        });
      }
      if (element[children] && element[children].length > 0) {
        let newarr = walker(element[children], label_text);
        result.concat(newarr);
      }
    });
  }
  walker(arr);
  return result;
}
